package com.atguigu.study.controller;

import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Base64;

@RestController
@Slf4j
public class ChatLanguageModelController {
    @Autowired
    private ChatModel chatModel;

    @Value("classpath:static/images/头像.jpg")
    private Resource resource;

    /**
     * http://localhost:9004/image/call
     */
    @GetMapping(value = "/image/call")
    public String readImageContent() throws IOException {
        byte[] byteArray = resource.getContentAsByteArray();
        String base64Data = Base64.getEncoder().encodeToString(byteArray);
        System.out.println("Base64Data：" + base64Data);

        UserMessage userMessage = UserMessage.from(
            TextContent.from("图片上的人物是谁"),
            ImageContent.from(base64Data, "image/png")
        );

        ChatResponse response = chatModel.chat(userMessage);

        System.out.println(response.aiMessage().text());

        return response.aiMessage().text();
    }
}
